<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="出行时间" prop="travelTime">
      <el-input v-model="dataForm.travelTime" placeholder="出行时间"></el-input>
    </el-form-item>
    <el-form-item label="出行目的" prop="travelPoint">
      <el-input v-model="dataForm.travelPoint" placeholder="出行目的"></el-input>
    </el-form-item>
    <el-form-item label="出行目的经度" prop="travelPointY">
      <el-input v-model="dataForm.travelPointY" placeholder="出行目的经度"></el-input>
    </el-form-item>
    <el-form-item label="出行目的维度" prop="travelPointX">
      <el-input v-model="dataForm.travelPointX" placeholder="出行目的维度"></el-input>
    </el-form-item>
    <el-form-item label="上车时间" prop="boardingTime">
      <el-input v-model="dataForm.boardingTime" placeholder="上车时间"></el-input>
    </el-form-item>
    <el-form-item label="上车地点" prop="boardingPoint">
      <el-input v-model="dataForm.boardingPoint" placeholder="上车地点"></el-input>
    </el-form-item>
    <el-form-item label="上车地点经度" prop="boardingPointY">
      <el-input v-model="dataForm.boardingPointY" placeholder="上车地点经度"></el-input>
    </el-form-item>
    <el-form-item label="上车地点维度" prop="boardingPointX">
      <el-input v-model="dataForm.boardingPointX" placeholder="上车地点维度"></el-input>
    </el-form-item>
    <el-form-item label="座位" prop="seats">
      <el-input v-model="dataForm.seats" placeholder="座位"></el-input>
    </el-form-item>
    <el-form-item label="价格/位" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格/位"></el-input>
    </el-form-item>
    <el-form-item label="发布出行单用户Id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="发布出行单用户Id"></el-input>
    </el-form-item>
    <el-form-item label="出行车辆Id" prop="carId">
      <el-input v-model="dataForm.carId" placeholder="出行车辆Id"></el-input>
    </el-form-item>
    <el-form-item label="出行单状态【0 新建 1 已出发 2 已完成  3 已过期】" prop="status">
      <el-input v-model="dataForm.status" placeholder="出行单状态【0 新建 1 已出发 2 已完成  3 已过期】"></el-input>
    </el-form-item>
    <el-form-item label="出行单备注信息" prop="note">
      <el-input v-model="dataForm.note" placeholder="出行单备注信息"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除【1 删除 0 正常】" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="是否删除【1 删除 0 正常】"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          travelTime: '',
          travelPoint: '',
          travelPointY: '',
          travelPointX: '',
          boardingTime: '',
          boardingPoint: '',
          boardingPointY: '',
          boardingPointX: '',
          seats: '',
          price: '',
          userId: '',
          carId: '',
          status: '',
          note: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          travelTime: [
            { required: true, message: '出行时间不能为空', trigger: 'blur' }
          ],
          travelPoint: [
            { required: true, message: '出行目的不能为空', trigger: 'blur' }
          ],
          travelPointY: [
            { required: true, message: '出行目的经度不能为空', trigger: 'blur' }
          ],
          travelPointX: [
            { required: true, message: '出行目的维度不能为空', trigger: 'blur' }
          ],
          boardingTime: [
            { required: true, message: '上车时间不能为空', trigger: 'blur' }
          ],
          boardingPoint: [
            { required: true, message: '上车地点不能为空', trigger: 'blur' }
          ],
          boardingPointY: [
            { required: true, message: '上车地点经度不能为空', trigger: 'blur' }
          ],
          boardingPointX: [
            { required: true, message: '上车地点维度不能为空', trigger: 'blur' }
          ],
          seats: [
            { required: true, message: '座位不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格/位不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '发布出行单用户Id不能为空', trigger: 'blur' }
          ],
          carId: [
            { required: true, message: '出行车辆Id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '出行单状态【0 新建 1 已出发 2 已完成  3 已过期】不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '出行单备注信息不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除【1 删除 0 正常】不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/travellist/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.travelTime = data.travelList.travelTime
                this.dataForm.travelPoint = data.travelList.travelPoint
                this.dataForm.travelPointY = data.travelList.travelPointY
                this.dataForm.travelPointX = data.travelList.travelPointX
                this.dataForm.boardingTime = data.travelList.boardingTime
                this.dataForm.boardingPoint = data.travelList.boardingPoint
                this.dataForm.boardingPointY = data.travelList.boardingPointY
                this.dataForm.boardingPointX = data.travelList.boardingPointX
                this.dataForm.seats = data.travelList.seats
                this.dataForm.price = data.travelList.price
                this.dataForm.userId = data.travelList.userId
                this.dataForm.carId = data.travelList.carId
                this.dataForm.status = data.travelList.status
                this.dataForm.note = data.travelList.note
                this.dataForm.createTime = data.travelList.createTime
                this.dataForm.updateTime = data.travelList.updateTime
                this.dataForm.isDeleted = data.travelList.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/travellist/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'travelTime': this.dataForm.travelTime,
                'travelPoint': this.dataForm.travelPoint,
                'travelPointY': this.dataForm.travelPointY,
                'travelPointX': this.dataForm.travelPointX,
                'boardingTime': this.dataForm.boardingTime,
                'boardingPoint': this.dataForm.boardingPoint,
                'boardingPointY': this.dataForm.boardingPointY,
                'boardingPointX': this.dataForm.boardingPointX,
                'seats': this.dataForm.seats,
                'price': this.dataForm.price,
                'userId': this.dataForm.userId,
                'carId': this.dataForm.carId,
                'status': this.dataForm.status,
                'note': this.dataForm.note,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
